
#include <BGridLayout>

#include "AlignWidget.h"

AlignWidget::AlignWidget(BWidget* parent) : BWidget(parent)
{
	_leftTopButton = new BButton("left-top");
	_leftTopButton->setAsset(Align_LeftTop);
	_leftTopButton->setForm(Form_IconAlone);

	_topCenterButton = new BButton("top-center");
	_topCenterButton->setAsset(Align_TopCenter);
	_topCenterButton->setForm(Form_IconAlone);

	_rightTopButton = new BButton("right-top");
	_rightTopButton->setAsset(Align_RightTop);
	_rightTopButton->setForm(Form_IconAlone);

	_leftCenterButton = new BButton("left-center");
	_leftCenterButton->setAsset(Align_LeftCenter);
	_leftCenterButton->setForm(Form_IconAlone);

	_centerButton = new BButton("center");
	_centerButton->setAsset(Align_Center);
	_centerButton->setForm(Form_IconAlone);

	_rightCenterButton = new BButton("right-center");
	_rightCenterButton->setAsset(Align_RightCenter);
	_rightCenterButton->setForm(Form_IconAlone);

	_leftBottomButton = new BButton("left-bottom");
	_leftBottomButton->setAsset(Align_LeftBottom);
	_leftBottomButton->setForm(Form_IconAlone);

	_bottomCenterButton = new BButton("bottom-center");
	_bottomCenterButton->setAsset(Align_BottomCenter);
	_bottomCenterButton->setForm(Form_IconAlone);

	_rightBottomButton = new BButton("right-bottom");
	_rightBottomButton->setAsset(Align_RightBottom);
	_rightBottomButton->setForm(Form_IconAlone);

	BGridLayout* gridLayout = new BGridLayout(this);
	gridLayout->setWidget(0, 0, _leftTopButton);
	gridLayout->setWidget(0, 1, _topCenterButton);
	gridLayout->setWidget(0, 2, _rightTopButton);
	gridLayout->setWidget(1, 0, _leftCenterButton);
	gridLayout->setWidget(1, 1, _centerButton);
	gridLayout->setWidget(1, 2, _rightCenterButton);
	gridLayout->setWidget(2, 0, _leftBottomButton);
	gridLayout->setWidget(2, 1, _bottomCenterButton);
	gridLayout->setWidget(2, 2, _rightBottomButton);
	gridLayout->setSpacing(10);

	_buttonGroup = new BButtonGroup();
	_buttonGroup->addButton(_leftTopButton);
	_buttonGroup->addButton(_topCenterButton);
	_buttonGroup->addButton(_rightTopButton);
	_buttonGroup->addButton(_leftCenterButton);
	_buttonGroup->addButton(_centerButton);
	_buttonGroup->addButton(_rightCenterButton);
	_buttonGroup->addButton(_leftBottomButton);
	_buttonGroup->addButton(_bottomCenterButton);
	_buttonGroup->addButton(_rightBottomButton);
	this->setAsset(_buttonGroup);

	connect(_buttonGroup, Signal_Checked, &AlignWidget::slotButtonChecked);
}
AlignWidget::~AlignWidget()
{

}

void AlignWidget::setAlign(Align align)
{
	if (align == Align_VCenter)
		align = Align_LeftCenter;
	if (align == Align_HCenter)
		align = Align_TopCenter;
	for (int i = 0; i < _buttonGroup->buttonCount(); i++)
	{
		BButton* button = _buttonGroup->button(i);
		Align flag = button->asset();
		if (align == flag)
		{
			button->setChecked(true);
			break;
		}
	}
}
Align AlignWidget::align() const
{
	if (BButton* button = _buttonGroup->checkedButton())
	{
		Align flag = button->asset();
		return flag;
	}
	return Align_None;
}
BString AlignWidget::alignText() const
{
	BString text;
	if (BButton* button = _buttonGroup->checkedButton())
	{
		Align flag = button->asset();
		if (flag == Align_None)
		{
			text = "none";
		}
		else if (flag == Align_Center)
		{
			text << "center";
		}
		else
		{
			if (flag & Align_Left)
				text << "left" << ',';
			if (flag & Align_Top)
				text << "top" << ',';
			if (flag & Align_Right)
				text << "right" << ',';
			if (flag & Align_Bottom)
				text << "bottom" << ',';
			else
			{
				if (flag & Align_VCenter)
					text << "vcenter" << ',';
				if (flag & Align_HCenter)
					text << "hcenter" << ',';
			}
		}
		text -= ',';
	}
	return text;
}

void AlignWidget::slotButtonChecked()
{
	if (BButton* button = _buttonGroup->checkedButton())
	{
		Align flag = button->asset();
		emit(Signal_AlignChanged, (Align)flag);
	}
}

